import { post, get, postParams } from '@/utils/http';
import type {
  IUserData,
  IUserParams,
  LoginResult,
  IResetUser,
  ModifyUser,
  PwdRetrieve,
  SendEmailCodeParams,
  IEnterprise
} from './model/userModel';
import config from '@/config';

enum Api {
  // 用户名密码登录
  ACCOUNT_LOGIN = '/upms/customer/login',
  JWT_LOGIN = '/upms/supplierUser/jwtLogin',
  // esp登录
  EPS_LOGIN = '/upms/customer/user/epsLogin',
  // geely用户中心登录
  GEELY_LOGIN = '/upms/customer/user/idaas/jwtLogin',
  // 飞书登录
  FEISHU_LOGIN = '/upms/customer/feishu/login',
  // 钉钉登录
  DINGTALK_LOGIN = '/upms/customer/dingtalk/login',
  // 获取用户信息
  GET_USER_INFO = '/upms/customer/getCustomerInfo',
  // 获取菜单信息
  GET_MENU = '/upms/menu/getUserMenu',
  // 编辑用户信息
  MODIFY_USER_INFO = '/upms/supplierUser/modifySupplierUserInfo',
  // 更改密码
  CHANGE_PWD = '/upms/user/forgetPwd/reset',
  // 重置密码
  RETRIEVE_PWD = '/upms/customer/pwdRetrieve',
  // 发送邮件验证码
  SEND_EMAIL_CODE = '/upms/message/emailCode',
  // 用户获取协议
  GET_PROTOCOL_URL = '/upms/protocol/getProtocolUrl',
  // 确认协议
  CHECK_PROTOCOL = '/upms/protocol/checkProtocol',

  /******** 供应商端 *******/
  // 供应商端登录
  SUPPLIER_LOGIN = '/upms/supplierUser/login',
  // 获取供应商用户信息
  GET_SUPPLIER_INFO = '/upms/supplierUser/getSupplierUserInfo',
  // 获取用户企业列表
  GET_ENTERPRISE_LIST = '/upms/customer/listUserEnterprise',
  // 切换组织
  SWITCH_ENTERPRISE = '/upms/customer/switchUserEnterprise',
  OA_LOGIN = '/upms/customer/sso/login'
}

/**
 * 图片验证码地址
 */

export const verifyCodeImgUrl = config.BASE_URL + '/code';

// NOTE 因为后端处理验证码在gateway中需要在url中传参，与账号密码不在一个地方处理。
/**
 * 用户名密码登录
 * @param data body入参
 * @param params url入参
 * @returns
 */
export const userAccountLogin = (data: IUserData, params: IUserParams): Promise<LoginResult> =>
  postParams(Api.ACCOUNT_LOGIN, data, params, true);

/**
 * eps登录
 * @param epssession token
 * @returns
 */
export const epsLogin = (epssession: string): Promise<LoginResult> =>
  post(Api.EPS_LOGIN, { epssession }, true, false);

/**
 * JWT登录
 * @param idToken token
 * @returns
 */
export const jwtLogin = (idToken: string): Promise<LoginResult> =>
  post(Api.JWT_LOGIN, { idToken }, true);
/**
 * oa登录
 * @param ticket ticket
 * @returns
 */
export const oaLoginApi = (ticket: string): Promise<LoginResult> =>
  post(Api.OA_LOGIN, { ticket }, true);

/**
 * geely用户中心登录
 * @param idToken token
 * @returns
 */
export const geelyLogin = (idToken: string): Promise<LoginResult> =>
  post(Api.GEELY_LOGIN, { idToken }, true, false);

/**
 * 飞书登录
 * @param code token
 * @returns
 */
export const feishuLogin = (code: string): Promise<LoginResult> =>
  post(Api.FEISHU_LOGIN, { code }, true, false);

/**
 * 钉钉登录
 * @param code token
 * @returns
 */
export const dingtalkLogin = (code: string): Promise<LoginResult> =>
  post(Api.DINGTALK_LOGIN, { code }, true, false);

/**
 * 获取用户信息
 * @returns
 */
export const getManagerInfo = () => get(Api.GET_USER_INFO);

/**
 * 获取菜单信息
 * @returns
 */
export const getMenu = () => get(Api.GET_MENU);

/**
 * 编辑用户信息
 * @param params 入参
 * @returns
 */
export const modifyUser = (params: ModifyUser) => post(Api.MODIFY_USER_INFO, params);

/**
 * 更改密码
 * @param params 入参
 * @returns
 */
export const changePwd = (params: IResetUser) => post(Api.CHANGE_PWD, params);

/**
 * 重置密码
 * @param params 入参
 * @returns
 */
export const pwdRetrieve = (params: PwdRetrieve) => post(Api.RETRIEVE_PWD, params, true);

/**
 * 发送邮件验证码
 * @param params 入参
 * @returns
 */
export const sendEmailCode = (params: SendEmailCodeParams) =>
  get(Api.SEND_EMAIL_CODE, params, true);

/**
 * 用户获取协议
 * @returns
 */
export const getProtocolUrl = () => get(Api.GET_PROTOCOL_URL, '', true);

/**
 * 确认协议
 * @returns
 */
export const checkProtocol = () => post(Api.CHECK_PROTOCOL);

/******** 供应商端 *******/
/**
 * 供应商端登录
 * @returns
 */
export const supplierLogin = (data: IUserData, params: IUserParams): Promise<LoginResult> =>
  postParams(Api.SUPPLIER_LOGIN, data, params, true);

/**
 * 获取供应商用户信息
 * @returns
 */
export const getSupplerInfo = () => get(Api.GET_SUPPLIER_INFO);

/**
 * 获取组织列表
 * @returns
 */
export const getEnterpriseList = (): Promise<IEnterprise[]> => get(Api.GET_ENTERPRISE_LIST);
export const switchEnterprise = (enterpriseNo: string) =>
  post(Api.SWITCH_ENTERPRISE, { enterpriseNo });
